package com.weishenai.Servlet;

import cn.hutool.json.JSONUtil;
import com.weishenai.Entity.UserInfo;
import com.weishenai.Service.UserInfoService;
import com.weishenai.Service.impl.UserInfoServiceImpl;
import com.weishenai.Utils.GetParamMeterMapUtils;
import com.weishenai.common.Result;
import org.jcp.xml.dsig.internal.dom.Utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Auther: weishenai
 * @Date: 2021/10/07/11:17
 * @Description:
 */
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        Map<String, Object> res = new HashMap<>();

        String search = req.getParameter("search");
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        UserInfoService userInfoService = new UserInfoServiceImpl();
        List<UserInfo> userInfos = userInfoService.getUserInfoList(search, pageSize, currentPage);
        int total = userInfoService.getUserInfoCount();

        res.put("userInfo", userInfos);
        res.put("total", total);

        System.out.println(JSONUtil.toJsonStr(Result.success(res
        )));
        out.println(JSONUtil.toJsonStr(Result.success(res)));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");
        int age = Integer.parseInt(req.getParameter("age"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");
        String sickId = req.getParameter("sickId");

        UserInfoService userInfoService = new UserInfoServiceImpl();
        UserInfo userInfo = new UserInfo(id, name, age, sex, address, sickId);
        int res = userInfoService.save(userInfo);
        if (res == 0) {
            out.println(JSONUtil.toJsonStr(Result.error("-1", "操作异常")));
        } else {
            out.println(JSONUtil.toJsonStr(Result.success(res)));
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = null;
        UserInfoService userInfoService = new UserInfoServiceImpl();
        PrintWriter out = resp.getWriter();
        try {
            InputStreamReader reader = new InputStreamReader(req.getInputStream());
            br = new BufferedReader(reader);

            String data = br.readLine();
            Map<String, String> paramMap = GetParamMeterMapUtils.getParamMeterMap(data);

            String id = paramMap.get("id");

            int affectRows = userInfoService.deleteById(id);
            if (affectRows == 0) {
                out.println(JSONUtil.toJsonStr(Result.error("-1", "无法删除")));
            } else {
                out.println(JSONUtil.toJsonStr(Result.success()));
            }
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (br != null) {
                try {
                    br.close();
                    out.flush();
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(Utils.class.getName()).log(Level.WARNING, null, ex);
                }
            }
        }
    }
}
